In this series of lectures we will be diving a little deeper into all the methods available in a list object. These aren't officially "advanced" features, just methods that you wouldn't typically encounter without some additional exploring. Its pretty likely that you've already encountered some of these yourself!
Lets begin!
In [1]:
l = [1,2,3]
In [2]:
l.append(4)
l
Out[2]:
In [4]:
l.count(10)
Out[4]:
In [5]:
l.count(2)
Out[5]:
In [6]:
x = [1, 2, 3]
x.append([4, 5])
print x
extend: extends list by appending elements from the iterable
In [7]:
x = [1, 2, 3]
x.extend([4, 5])
print x
Note how extend append each element in that iterable. That is the key difference.
In [10]:
l.index(2)
Out[10]:
In [11]:
l.index(12)
In [12]:
l
Out[12]:
In [13]:
# Place a letter at the index 2
l.insert(2,'inserted')
In [14]:
l
Out[14]:
In [15]:
ele = l.pop()
In [16]:
l
Out[16]:
In [17]:
ele
Out[17]:
In [18]:
l
Out[18]:
In [19]:
l.remove('inserted')
In [20]:
l
Out[20]:
In [21]:
l = [1,2,3,4,3]
In [22]:
l.remove(3)
In [23]:
l
Out[23]:
In [24]:
l.reverse()
In [25]:
l
Out[25]:
In [26]:
l
Out[26]:
In [27]:
l.sort()
In [28]:
l
Out[28]:
Great! You should now have an understanding of all the methods available for a list in Python!